package com.cdshangpai.utils.storage;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import com.cdshangpai.photoapp.LogConfig;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 适应android10的自适应文件写入操作
 */
public class FileOutput extends FileBase{
    FileOutputStream mFos;
    OutputStream mos;
    Uri uri;
    String mFileName;

    /**
     * 初始化对象，创建文件输出流
     * @param folder
     * @param fileName
     */
    public FileOutput(Context context,String folder,String fileName)
    {
        super(context);
        this.mFileName=fileName;
        if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
        {
            String pathName= Environment.getExternalStorageDirectory()+File.separator+folder;
            File path=new File(pathName);
            if(!path.exists())
            {
                path.mkdirs();
            }
            File file=new File(pathName,fileName);
            try{
                mFos=new FileOutputStream(file);
                Log.i(LogConfig.logTag,"创建输入文件成功，文件位置:"+file.getAbsolutePath());
            }catch (Exception e){
                Log.i(LogConfig.logTag,"创建输出文件失败,文件位置:"+file.getAbsolutePath()+"原因"+e.getMessage());
            }
        }
        else
        {
            uri=getFileUrl(folder,fileName);
            try{
                mos=resolver.openOutputStream(uri);
            }catch (Exception e){}
        }
    }

    public FileOutput(Context context,String folder,String fileName,Boolean forrewrite)
    {
        super(context);
        this.mFileName=fileName;
        if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
        {
            String pathName= Environment.getExternalStorageDirectory()+File.separator+folder;
            File path=new File(pathName);
            if(!path.exists())
            {
                path.mkdirs();
            }
            File file=new File(pathName,fileName);
            try{
                mFos=new FileOutputStream(file);
                Log.i(LogConfig.logTag,"创建输入文件成功，文件位置:"+file.getAbsolutePath());
            }catch (Exception e){
                Log.i(LogConfig.logTag,"创建输出文件失败,文件位置:"+file.getAbsolutePath()+"原因"+e.getMessage());
            }
        }
        else
        {
            uri = Uri.parse(fileName);
            try {
                mos = mContext.getContentResolver().openOutputStream(uri);
            } catch (Exception e) {
            }
        }
    }



    public Boolean isSeccess()
    {
        if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
        {
            return mFos!=null;
        }
        else
        {
            return mos!=null;
        }
    }

    public String getUriString()
    {
        return  uri.toString();
    }

    /**
     * 返回 操作文件名
     * @return
     */
    public String pickFileName()
    {
        if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
        {
            return this.mFileName;
        }else
        {
            return getUriString();
        }
    }



    /**
     * 写入文件
     * @param data
     * @return
     * @throws Exception
     */
    public boolean write(byte[] data)
    {
        try
        {
            if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
            {
                mFos.write(data);
            }
            else
            {
                mos.write(data);
            }
            return true;
        }catch (Exception e){}
        return true;
    }

    public boolean write(byte[] data,Integer offset,Integer len)
    {
        try
        {
            if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
            {
                mFos.write(data,offset,len);
            }
            else
            {
                mos.write(data,offset,len);
            }
            return true;
        }catch (Exception e){}
        return true;
    }

    public void flush()
    {
        try
        {
            if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
            {
                mFos.flush();
            }
            else
            {
                mos.flush();
            }
        }catch (Exception e){}
    }

    /**
     * 写入操作完成后，关闭输出流
     */
    public void close()
    {
        try{
            if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
            {
                mFos.close();
            }
            else
            {
                mos.close();
            }
        }catch (Exception e){}
    }

    public OutputStream getOutputStream()
    {
        if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
        {
            return mFos;
        }else
        {
            return mos;
        }
    }
}
